<?php

class Application_Model_Books extends Application_Model_Base{
    private static $instance = null;
    public static function getInstance(){
        if (null === self::$instance) self::$instance = new self();
        return self::$instance;
    }
    private function __clone() {}
    private function __construct() {}

    public function getPaginatorForFilter($params){
        $select = $this->getDBAdapter()->select()
            ->from('books');
        if(!empty($params['book_name'])){
            $select->where("`book_name` LIKE '%{$params['book_name']}%'");
        }
        if(!empty($params['fk_editions_id'])){
            $select->where("`fk_editions_id` LIKE '%{$params['fk_editions_id']}%'");
        }
        if(!empty($params['fk_authors_id'])){
            $select->where("`fk_authors_id` LIKE '%{$params['fk_authors_id']}%'");
        }
        if(!empty($params['fk_book_languages_id'])){
            $select->where("`fk_book_languages_id` LIKE '%{$params['fk_book_languages_id']}%'");
        }
        if(!empty($params['book_count_pages'])){
            $select->where("`book_count_pages` LIKE '%{$params['book_count_pages']}%'");
        }
        if(!empty($params['book_number_of_copies'])){
            $select->where("`book_number_of_copies` LIKE '%{$params['book_number_of_copies']}%'");
        }
        if(!empty($params['book_year_of_publication'])){
            $select->where("`book_year_of_publication` LIKE '%{$params['book_year_of_publication']}%'");
        }
        $paginatorAdapter = new Zend_Paginator_Adapter_DbSelect($select);
        return new Zend_Paginator($paginatorAdapter);
    }

    public function getAllForSelect(){
        $books = $this->getDBAdapter()->query("
            select `book_id`, `book_name` from `books`
        ")->fetchAll();
        $booksForSelect = array();
        foreach($books as $book){
            $booksForSelect[$book['book_id']] = $book['book_name'];
        }
        return $booksForSelect;
    }

    public function add($data){
        $this->getDBAdapter()->insert('books', $data);
    }
    }